<template>
	<view>
		<uni-calendar
			:insert="true"
			:start-date="curTime"
			:end-date="curTime"
			:selected="selectedArr"
			ref="calendar"
		/>
		<view class="foot-button">
			<u-button
				type="primary"
				@click="openPop"
				class="u-button"
				v-if="!selectedArr.some((item) => item.date === curTime)"
				>签到</u-button
			>
			<u-button type="primary" @click="cancel" class="u-button" v-else
				>取消签到</u-button
			>
		</view>

		<u-action-sheet
			:actions="list"
			:show="show"
			:closeOnClickAction="true"
			:closeOnClickOverlay="true"
			@close="close"
			@select="select"
		></u-action-sheet>
	</view>
</template>

<script lang="ts" setup>
import dayjs from "dayjs";
import { useCookingStore } from "@/store/modules/cooking";

const cookingStore = useCookingStore();

let curTime = dayjs().format("YYYY-MM-DD");
let selectedArr = cookingStore.selectList;

let list = [
	{
		name: "蔡竞"
	},
	{
		name: "刘琦"
	}
];

let show = ref(false);
const calendar = ref();
const openPop = () => {
	show.value = true;
};

const close = () => {
	show.value = false;
};

const select = (e: any) => {
	if (!selectedArr.some((item) => item.date === curTime)) {
		selectedArr.push({
			date: curTime,
			info: e.name
		});
		cookingStore.setCookingList(selectedArr);
	}
};
const cancel = () => {
	uni.showModal({
		title: "提示",
		content: "确定取消么",
		showCancel: true,
		success: ({ confirm }) => {
			if (confirm) {
				let index = selectedArr.findIndex((item) => item.date === curTime);

				if (index != -1) {
					selectedArr.splice(index, 1);

					cookingStore.setCookingList(selectedArr);
				}
			}
		}
	});
};
</script>

<style lang="scss" scoped>
.foot-button {
	margin-top: 30rpx;
	padding: 0 80rpx;
	display: flex;
	justify-content: center;
	align-items: center;
}
</style>
